package spring.cloud.tasks.client.item_task_processor.pooled.timer_task;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.client.item_task_processor.TaskProcessor;
import spring.cloud.tasks.client.item_task_processor.TaskProcessorInfo;
import spring.cloud.tasks.client.item_task_processor.pooled.TaskPauseOrResumeScheduleTaskPooled;
import spring.cloud.tasks.client.item_task_processor.pooled.impl.TaskPauseOrResumeScheduleTaskPooledImpl;
import spring.cloud.tasks.client.util.CronExpression;

import java.util.Date;

@Setter
@Getter
@Slf4j
public class PauseOrResumeScheduleTask implements Runnable {
    public static final int TYPE_PAUSE = 1;
    public static final int TYPE_RESUME = 2;
    private TaskPauseOrResumeScheduleTaskPooled taskPauseOrResumeScheduleTaskPooled;
    private String processorId;
    private TaskProcessor taskProcessor;
    private TaskProcessorInfo taskProcessorInfo;
    private String cronExpressionString;
    private int type;

    public PauseOrResumeScheduleTask(TaskProcessor taskProcessor, TaskProcessorInfo taskProcessorInfo, int type, String cronExpressionString) {
        this.processorId = taskProcessorInfo.getProcessorId();
        this.taskProcessor = taskProcessor;
        this.taskProcessorInfo = taskProcessorInfo;
        this.type = type;
        this.cronExpressionString = cronExpressionString;
    }

    @Override
    public void run() {
        //调度的优先级更高
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        Date nowDate = new Date();
        CronExpression cronExpression = new CronExpression(cronExpressionString);
        Date nextRunTime = cronExpression.getNextTimeAfter(nowDate);
        if (this.type == TYPE_PAUSE) {
            this.taskProcessor.getTaskProcessorInfo().setNextPauseTime(nextRunTime);
            this.taskProcessor.setNextPauseTime(nextRunTime);
        } else {
            this.taskProcessor.getTaskProcessorInfo().setNextRunTime(nextRunTime);
            this.taskProcessor.setNextRunTime(nextRunTime);
        }
        try {
            if (taskProcessor.isScheduleTask()) {//允许被开始调度
                if (this.type == TYPE_PAUSE) {
                    if (!taskProcessor.needReInitProcessor) {//不需要暂停
                        boolean stopResult = taskProcessor.pause("");
                        if (stopResult) {
                            this.taskProcessorInfo.setMessage("到达终止时间,暂停调度!");
                            log.debug("到达终止时间,pause调度!");
                        }
                    }
                } else {
                    if (taskProcessor.needReInitProcessor) {//不需要重启
                        boolean startResult = taskProcessor.resume(true, "resume");
                        this.taskProcessorInfo.setNextRunTime(nextRunTime);
                        log.debug("到达开始时间,resume调度,nextRunTimeString:{}", nextRunTime);
                        if (startResult) {
                            this.taskProcessorInfo.setMessage("到达开始时间,恢复调度!");
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (this.type == TYPE_PAUSE) {
                taskPauseOrResumeScheduleTaskPooled.schedule(TaskPauseOrResumeScheduleTaskPooledImpl.pause, this, nextRunTime);
            } else {
                taskPauseOrResumeScheduleTaskPooled.schedule(TaskPauseOrResumeScheduleTaskPooledImpl.resume, this, nextRunTime);
            }

        }
    }
}
